package cn.hnu.jpa.jpaExample.ManyToMany;

import static org.junit.Assert.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.Test;

import cn.hnu.jpa.jpaExample.ManyToOne.SingleWay.ManyAddress;
import cn.hnu.jpa.jpaExample.ManyToOne.SingleWay.ManyCustomer;

public class SingleUnitTest {

	//@Test //测试多对多单向
	public void test() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		MMSingleCustomer cpk1 = new MMSingleCustomer();
		MMSingleCustomer cpk2 = new MMSingleCustomer();
		MMSingleAddr addr1 = new MMSingleAddr();
		MMSingleAddr addr2 = new MMSingleAddr();
		cpk1.setName("zdc1");
		cpk2.setName("zdc2");
		addr1.setAddr("1111111");
		addr2.setAddr("2222222");
		cpk2.addAddr(addr1);
		cpk2.addAddr(addr2);
		cpk1.addAddr(addr1);
		cpk1.addAddr(addr2);
		
		em.persist(cpk2);
		em.persist(cpk1);
		em.getTransaction().commit();
		em.close();
		emf.close(); 
	}
//	@Test //测试多对多双向
//		public void test2() {
//			EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa");
//			EntityManager em = emf.createEntityManager();
//			em.getTransaction().begin();
//			MMSingleCustomer cpk1 = new MMSingleCustomer();
//			MMSingleCustomer cpk2 = new MMSingleCustomer();
//			MMSingleAddr addr1 = new MMSingleAddr();
//			MMSingleAddr addr2 = new MMSingleAddr();
//			cpk1.setName("zdc1");
//			cpk2.setName("zdc2");
//			addr1.setAddr("1111111");
//			addr2.setAddr("2222222");
//			cpk2.addAddr(addr1);
//			cpk2.addAddr(addr2);
//			cpk1.addAddr(addr1);
//			cpk1.addAddr(addr2);
//			
//			em.persist(cpk2);
//			em.persist(cpk1);
//			em.getTransaction().commit();
//			em.close();
//			emf.close(); 
//		}
}
